package com.ly.mpayment.persist.task;

import com.ly.mpayment.core.util.CookieRedis;
import com.ly.mpayment.persist.service.downshopinfo.IDownShopInfoService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 定时扫描系统余额 卡单 并且发送通知
 */
@Service
@Component("taskScanService")
public class TaskScan {

    private String company = "快快收话费平台:";


    @Autowired
    private IDownShopInfoService downShopInfoService;

    /**
     * 定时扫描 下游余额 上游余额 人工订单 卡单超过10分钟 提醒回款
     */
    public void task(){
        try{
            downshopbalance();
        }catch (Exception e){
            e.printStackTrace();
        }
        try{
            upshopbalance();
        }catch (Exception e){
            e.printStackTrace();
        }
        try{
            merchantcredit();
        }catch (Exception e){
            e.printStackTrace();
        }
        try{
            this.downArtificial();
        }catch (Exception e){
            e.printStackTrace();
        }
        try{
            this.upArtificial();
        }catch (Exception e){
            e.printStackTrace();
        }
        try{
            this.downStreamcard();
        }catch (Exception e){
            e.printStackTrace();
        }
        try{
            this.upOrderMinute();
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    public void sendMsg(String content){
        EnterpriseWeChatRobotHandler.handler(CookieRedis.get("webHook"), content);
    }
    /**
     * 下游余额提醒
     */
    public void downshopbalance(){
        List<JSONObject> jsonObjectList = downShopInfoService.findDownshopBalance();
        if(!CollectionUtils.isEmpty(jsonObjectList)){
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(company);
            stringBuilder.append("\n");
            for(JSONObject jsonObject : jsonObjectList){
                stringBuilder.append(jsonObject.getString("username"));
                stringBuilder.append("余额:");
                stringBuilder.append(jsonObject.getString("downshopbalance"));
                stringBuilder.append("\n");
            }
            stringBuilder.append("下游余额不足,请及时联系加款");
            sendMsg(stringBuilder.toString());
        }
    }


    /**
     * 上游余额提醒
     */
    public void upshopbalance(){
        List<JSONObject> jsonObjectList = downShopInfoService.findUpshopBalance();
        if(!CollectionUtils.isEmpty(jsonObjectList)){
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(company);
            stringBuilder.append("\n");
            for(JSONObject jsonObject : jsonObjectList){
                stringBuilder.append(jsonObject.getString("username"));
                stringBuilder.append("余额:");
                stringBuilder.append(jsonObject.getString("upshopbalance"));
                stringBuilder.append("\n");
            }
            stringBuilder.append("上游余额不足,请及时联系加款");
            sendMsg(stringBuilder.toString());
        }
    }

    /**
     * 授信回款
     */
    public void merchantcredit(){
        List<JSONObject> jsonObjectList = downShopInfoService.findMerchantcredit();
        if(!CollectionUtils.isEmpty(jsonObjectList)){
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(company);
            stringBuilder.append("\n");
            for(JSONObject jsonObject : jsonObjectList){
                stringBuilder.append(jsonObject.getString("username"));
                stringBuilder.append("余额:");
                stringBuilder.append(jsonObject.getString("downshopbalance"));
                stringBuilder.append("\n");
            }
            stringBuilder.append("授信超额,请及时联系回款");
            sendMsg(stringBuilder.toString());
        }
    }

    /**
     * 下游人工
     */
    public void downArtificial(){
        List<JSONObject> jsonObjectList = downShopInfoService.downArtificial();
        if(!CollectionUtils.isEmpty(jsonObjectList)){
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(company);
            stringBuilder.append("\n");
            for(JSONObject jsonObject : jsonObjectList){
                stringBuilder.append(jsonObject.getString("username"));
                stringBuilder.append("人工卡单:");
                stringBuilder.append(jsonObject.getString("counts"));
                stringBuilder.append("笔\n");
            }
            stringBuilder.append("请及时处理");
            sendMsg(stringBuilder.toString());
        }
    }

    /**
     * 上游人工
     */
    public void upArtificial(){
        List<JSONObject> jsonObjectList = downShopInfoService.upArtificial();
        if(!CollectionUtils.isEmpty(jsonObjectList)){
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(company);
            stringBuilder.append("\n");
            for(JSONObject jsonObject : jsonObjectList){
                stringBuilder.append(jsonObject.getString("username"));
                stringBuilder.append("人工卡单:");
                stringBuilder.append(jsonObject.getString("counts"));
                stringBuilder.append("笔\n");
            }
            stringBuilder.append("请及时处理");
            sendMsg(stringBuilder.toString());
        }
    }

    /**
     * 上游卡单
     */
    public void downStreamcard(){
        List<JSONObject> jsonObjectList = downShopInfoService.downStreamcard();
        if(!CollectionUtils.isEmpty(jsonObjectList)){
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(company);
            stringBuilder.append("\n");
            for(JSONObject jsonObject : jsonObjectList){
                stringBuilder.append(jsonObject.getString("username"));
                stringBuilder.append("订单卡单:");
                stringBuilder.append(jsonObject.getString("counts"));
                stringBuilder.append("笔\n");
            }
            stringBuilder.append("请及时处理超时订单");
            sendMsg(stringBuilder.toString());
        }
    }

    /**
     * 连续失败订单提醒
     */
    public void upOrderMinute(){
        List<JSONObject> jsonObjectList = downShopInfoService.upOrderMinute();
        if(!CollectionUtils.isEmpty(jsonObjectList)){
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(company);
            stringBuilder.append("\n");
            for(JSONObject jsonObject : jsonObjectList){
                stringBuilder.append(jsonObject.getString("username"));
                stringBuilder.append("有连续失败的订单");
                stringBuilder.append("\n");
            }
            stringBuilder.append("请及时处理");
            sendMsg(stringBuilder.toString());
        }
    }

}
